﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;

namespace University.Workshop.DataAccess
{
    public class DataAccessWaterCollectControl
    {

        public static ICollection<WaterCollectControl> GetBetweenDates(DateTime initialDate, DateTime endDate)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.WaterCollectControl
                    where c.CollectPayDate >= initialDate && c.CollectPayDate <= endDate && c.IsPaid
                    select c).ToList();
        }

        public static ICollection<WaterCollectControl> SearchByClientId(Model.CollectControl chargetoFind)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.WaterCollectControl
                    where c.KardexId == chargetoFind.KardexId && c.IsPaid == false
                    select c).ToList();
        }

        public static void UpdateCollect(Model.CollectControl collectToUpdate)
        {
            WFPEntities.Instance.DiscardChanges();

            Model.WaterCollectControl cc = (from wp in WFPEntities.Instance.WaterCollectControl
                                            where wp.CollectId == collectToUpdate.CollectId
                                            select wp).First();

            cc.CollectPayDate = collectToUpdate.CollectPayDate;
            cc.IsPaid = true;

            WFPEntities.Instance.SaveChanges();
        }

        public static void RegisterNewWaterCollectControl(Model.WaterCollectControl waterCollect)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.WaterCollectControl.Add(waterCollect);
            WFPEntities.Instance.SaveChanges();
        }

        internal static ICollection<WaterCollectControl> GetNoPaid(Model.Kardex kardex)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from c in WFPEntities.Instance.WaterCollectControl
                    where c.KardexId == kardex.KardexId && c.IsPaid == false
                    select c).ToList();
        }

        public static ICollection<WaterCollectControl> GetWaterCollects(DateTime date)
        {
            return (from wc in WFPEntities.Instance.WaterCollectControl
                    where wc.IsPaid == false && wc.ReadingDate.Month == date.Month && wc.ReadingDate.Year == date.Year
                    select wc).ToList ();
        }

        public static bool ExistAnyWaterCollectWithThisKardexIDAndDate(int kardexID, DateTime date)
        {
            var collects = (from wc in WFPEntities.Instance.WaterCollectControl
                            where wc.KardexId == kardexID && wc.ReadingDate.Month == date.Month && wc.ReadingDate.Year == date.Year
                            select wc).ToList ();

            return (collects.Count > 0 ? true : false);
        }
    }
}
